package com.project.dao.imp;

import com.project.bean.StudentBean;
import com.project.dao.IStudentDao;

import java.time.LocalDate;
import java.util.ArrayList;
import java.util.List;

public class IStudentImp extends BaseDao implements IStudentDao {
    @Override
    public void add(StudentBean studentBean) {
        updateData("INSERT INTO t_student(studentName,phone,birthday,grade) VALUES (?,?,?,?)",
                studentBean.getStudentName(), studentBean.getPhone(), studentBean.getBirthday(), studentBean.getGrade());
    }

    @Override
    public void del(Integer id) {
        updateData("DELETE FROM t_student WHERE id = ?", id);
    }

    @Override
    public void update(Integer id, String phone) {
        updateData("UPDATE t_student SET phone= ? WHERE id = ? ", phone, id);
    }

    @Override
    public StudentBean findById(Integer id) {
        List<StudentBean> list  = findData("select * from t_students where id = ?",StudentBean.class, id);
        if (list.size()!=0){
            return list.get(0);
        }
        return null;
    }

    @Override
    public List<StudentBean> findAll() {
        return null;
    }

    @Override
    public List<StudentBean> findByItem(String name, LocalDate startDate, LocalDate endDate) {
        String sql = "SELECT * FROM t_student WHERE 1=1 ";
        List valueList = new ArrayList<>();
        if (name != null && name.length() != 0) {
            sql += "and studentName like ?";
            valueList.add("%" + name + "%");
        }
        if (startDate != null) {
            sql += "birthday >=?";
            valueList.add(startDate);
        }
        if (endDate != null) {
            sql += "birthday <=?";
            valueList.add(endDate);
        }
        return this.findData(sql, StudentBean.class, valueList.toArray());
    }
}
